import { defineStore } from 'pinia'
import { dietApi } from '@/api'

export const useDietStore = defineStore('diet', {
  state: () => ({
    dietRecords: [],
    currentRecord: null,
    loading: false
  }),
  
  getters: {
    // 获取最近的饮食记录
    recentRecords: (state) => {
      return state.dietRecords.slice(0, 10)
    },
    
    // 获取每日卡路里摄入
    dailyCalories: (state) => {
      const today = new Date().toISOString().split('T')[0]
      const todayRecords = state.dietRecords.filter(record => 
        record.recordDate === today
      )
      return todayRecords.reduce((total, record) => {
        return total + (record.calories || 0)
      }, 0)
    }
  },
  
  actions: {
    // 创建饮食记录
    async createRecord(recordData) {
      this.loading = true
      try {
        const response = await dietApi.createRecord(recordData)
        this.dietRecords.unshift(response)
        this.currentRecord = response
        return response
      } catch (error) {
        console.error('创建饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 更新饮食记录
    async updateRecord(recordId, recordData) {
      this.loading = true
      try {
        const response = await dietApi.updateRecord(recordId, recordData)
        // 更新本地状态
        const index = this.dietRecords.findIndex(record => record.recordId === recordId)
        if (index !== -1) {
          this.dietRecords[index] = response
        }
        if (this.currentRecord && this.currentRecord.recordId === recordId) {
          this.currentRecord = response
        }
        return response
      } catch (error) {
        console.error('更新饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取某日期的饮食记录
    async getRecords(date) {
      this.loading = true
      try {
        const response = await dietApi.getRecords(date)
        this.dietRecords = response
        return response
      } catch (error) {
        console.error('获取饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取日期范围内的饮食记录
    async getRecordsByDateRange(startDate, endDate) {
      this.loading = true
      try {
        const response = await dietApi.getRecordsByDateRange(startDate, endDate)
        this.dietRecords = response
        return response
      } catch (error) {
        console.error('获取饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取最近的饮食记录
    async getRecentRecords(limit = 10) {
      this.loading = true
      try {
        const response = await dietApi.getRecentRecords(limit)
        this.dietRecords = response
        return response
      } catch (error) {
        console.error('获取饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 删除饮食记录
    async deleteRecord(recordId) {
      this.loading = true
      try {
        await dietApi.deleteRecord(recordId)
        // 从本地状态中移除
        this.dietRecords = this.dietRecords.filter(record => record.recordId !== recordId)
        if (this.currentRecord && this.currentRecord.recordId === recordId) {
          this.currentRecord = null
        }
      } catch (error) {
        console.error('删除饮食记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 清空当前记录
    clearCurrentRecord() {
      this.currentRecord = null
    }
  }
})